package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 部门管理控制层
 */
@Slf4j
@RestController
@RequestMapping("/depts")
@SuppressWarnings("all")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable Integer id){
        log.info("删除部门id为：{}",id);
        deptService.deleteDept(id);
        return Result.success();
    }
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("修改部门信息为：{}",dept);
        deptService.updateDept(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectId(@PathVariable("id") Integer id){
        log.info("查询部门id为：{}",id);
        Dept dept =deptService.selectId(id);
        return Result.success(dept);
    }

    @GetMapping
    public  Result page(
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer pageSize) {
        PageResult<Dept> PageResult=deptService.page(name, status, page, pageSize);
        log.info("分页查询部门,参数:部门名称={},部门状态={},当前页码={},每页大小={}", name, status, page, pageSize);
        return Result.success(PageResult);

    }

    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        deptService.addDept(dept);
        return Result.success();
    }
}
